﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoccerSimulator5000
{
    /// <summary>
    /// This class holds information on a Seasons fixtures.
    /// The class contains a number of private ints.
    /// These ints exists only to aid in the generation of fixtures.
    /// By using these variables and by assigning them values as per the constructor, the class can generate fixtures for any (even numbered) amount of teams in the league.
    /// </summary>
    public class Fixtures
    {
        public List<Round> fixtures { get; set; }
        int numberOfTeams;
        int numberOfRounds;
        int matchesPerRound;
        int halfSize;

        public Fixtures()
        {
            
            //fixtures = new List<Round>();
            //numberOfTeams = Game.teamList.Count();
            //halfSize = numberOfTeams / 2;
            //numberOfRounds = (numberOfTeams - 1) * 2;
            //matchesPerRound = numberOfTeams / 2;
        }

        public void Init()
        {
            fixtures = new List<Round>();
            numberOfTeams = Game.teamList.Count();
            halfSize = numberOfTeams / 2;
            numberOfRounds = (numberOfTeams - 1) * 2;
            matchesPerRound = numberOfTeams / 2;
        }

        public void GenerateFixtures()
        {
            Game.schedule.generateSchedule();
            List<Team> teams = new List<Team>();
            List<Team> temp = new List<Team>();

            teams.AddRange(Game.teamList);
            temp.AddRange(Game.teamList);
            teams.RemoveAt(0);


            numberOfTeams = teams.Count;

            for (int round = 0; round < numberOfRounds; round++)
            {
                fixtures.Add(new Round());
                if (round % 2 == 0)
                {
                    int teamIdx = round % numberOfTeams;

                    fixtures[round].round.Add(new Match(teams[teamIdx], temp[0], round + 1));

                    for (int idx = 0; idx < halfSize; idx++)
                    {
                        int firstTeam = (round + idx) % numberOfTeams;
                        int secondTeam = ((round + numberOfTeams) - idx) % numberOfTeams;

                        if (firstTeam != secondTeam)
                        {
                            fixtures[round].round.Add(new Match(teams[firstTeam], teams[secondTeam], round + 1));
                        }
                    }
                }

                if ( round % 2 != 0 )
                {
                    int teamIdx = round % numberOfTeams;

                    fixtures[round].round.Add(new Match(temp[0], teams[teamIdx], round + 1));

                    for (int idx = 0; idx < halfSize; idx++)
                    {
                        int firstTeam = (round + idx) % numberOfTeams;
                        int secondTeam = ((round + numberOfTeams) - idx) % numberOfTeams;

                        if (firstTeam != secondTeam)
                        {
                            fixtures[round].round.Add(new Match(teams[secondTeam], teams[firstTeam], round + 1));
                        }
                    }
                    

                }
                
               
            }

            PrintFixturesToConsole();
            
        }


        public void PrintFixturesToConsole()
        {

            for (int i = 0; i < numberOfRounds; i++)
            {
                for( int j = 0; j < matchesPerRound; j++)
                {
                    fixtures[i].round[j].PrintMatchToConsole();
                }

            }
            
        }
    }
}
